// components/comment/index.js
import api from '../../api/index';

import {
  uploadFileTest
} from '../../utils/util'




Component({
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.setData({
        [`formData.forumId`]: this.data.detail.id,
        [`formData.type`]: this.data.type
      })
      this.getList();
    },
    moved: function () {},
    detached: function () {},
  },
  /**
   * 组件的属性列表
   */
  properties: {
    detail: Object,
    type: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    isLoading: false,
    isComplete: false,
    isShowMore: false, //是否展示更多评论
    currentIndex: '', //当前钱评论下标
    formData: {
      forumId: '', //帖子id
      eid: 0, //是否限制楼主>0限制
      pid: 0, //父id,默认0
      sortId: 0, //排序限制 id
      sortType: 1, //排序类型 0最早默认(id正序), 1最新(id倒序,需要限制id), 2最热(评论数倒序)
      pageNo: 1,
      type: 0, //类型，0赚钱社区1经纪人学院
    },
    isShowInput: false, //是否显示输入框
    tempFilePaths: [],
    value: '', //输入的内容
    currentComments: {}, //当前级评论信息/一级评论内容
    currentIndex: '', //当前评论下标/一级评论下标
    subComments: {}, //二级级内容/弹层评论内容
    subIndex: -1, //二级下标/弹层评论下标
    level3CommentsId: '', //三级评论id
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //切换排序状态
    onChangeSort(e) {
      this.setData({
        [`formData.sortType`]: e.currentTarget.dataset.type,
        [`formData.pageNo`]: 1,
        list: [],
        isLoading: false,
        isComplete: false,
      })
      this.getList();
    },
    //二级评论
    secondaryComments(e) {
      let data = e.currentTarget.dataset;
      console.log(data)
      if (data.subcontent) {
        //三级评论
        this.setData({
          subComments: data.subcontent,
          subIndex: data.index,
          isShowInput: true,
          level3CommentsId: data.commentid
        })
      } else {
        //二级评论
        this.setData({
          isShowInput: true,
          currentComments: data.item,
          currentIndex: data.index,
        })
      }
    },
    //上传图片
    //使用同步上传

    async uploadImg(urls) {
      let arrUrl = [];
      for (let index = 0; index < urls.length; index++) {
        let ossRes = await api.getOssAuth({
          dir: 'COMMENT_DIR'
        });
        let credentials = ossRes.data.data;
        let url = urls[index];
        let arr = url.split('.'); //截取后缀
        let suffix = arr[arr.length - 1];
        // console.log(suffix)
        console.log()
        let rt = await uploadFileTest(url, credentials, suffix)
        if (rt) {
          arrUrl.push(rt)
        }
      }
      return arrUrl
    },
    //发送
    async send() {

      let subPid = this.data.subComments.id;
      console.log(this.data.subComments)
      let pid = this.data.currentComments.id;
      //是否图片评论
      if (this.data.tempFilePaths.length) {
        let url = await this.uploadImg(this.data.tempFilePaths)
        url = url.join(',');
        console.log(url);
        let data = {
          "pid": subPid ? subPid : pid, //父级评论id
          "fid": this.data.detail.id, //帖子id
          "type": this.data.type, //评论类型
          "content": this.data.value, //评论内容
          url,
          replyId: this.data.level3CommentsId,
        }
        //图片加文字
        api.addComment(data).then(res => {
          wx.showToast({
            title: res.data.message,
          })
          this.setData({
            isShowInput: false,
          })
          // if (this.data.currentComments.id) {
          //   //二级评论
          //   let arr = this.data.list;
          //   let index = this.data.currentIndex;
          //   arr[this.data.currentIndex].remnant = ++arr[this.data.currentIndex].remnant;
          //   let currentComments = this.data.currentComments; //当前评论
          //   //是否展开二级评论弹层
          //   if (this.data.isShowMore) {
          //     if (this.data.subIndex >= 0) { //是否点击二级评论
          //       console.log('回复二级评论')
          //       arr[index].listForumCommentPageVo[this.data.subIndex].listForumCommentPageVo.unshift(res.data.data);
          //       currentComments.listForumCommentPageVo[this.data.subIndex].listForumCommentPageVo.unshift(res.data.data);
          //       //三级评论
          //       this.setData({
          //         list: arr,
          //         isShowInput: false,
          //         value: '',
          //         currentComments,
          //         tempFilePaths: [],
          //         subComments: {},
          //         subIndex: -1,
          //       })
          //     } else {
          //       arr[index].listForumCommentPageVo.unshift(res.data.data);
          //       currentComments.listForumCommentPageVo.unshift(res.data.data);
          //       console.log('在二级评论回复一级品论')
          //       //二级评论
          //       this.setData({
          //         list: arr,
          //         isShowInput: false,
          //         value: '',
          //         currentComments,
          //         tempFilePaths: [],
          //         subComments: {},
          //       })
          //     }
          //   } else {
          //     arr[this.data.currentIndex].listForumCommentPageVo.unshift(res.data.data);
          //     this.setData({
          //       list: arr,
          //       isShowInput: false,
          //       value: '',
          //       tempFilePaths: [],
          //     })
          //   }

          // } else {
          //   //一级评论
          //   let arr = this.data.list;
          //   arr.unshift(res.data.data)
          //   this.setData({
          //     list: arr,
          //     isShowInput: false,
          //     tempFilePaths: [],
          //     value: '',
          //     ['detail.replyNum']: ++this.data.detail.replyNum
          //   })

          // }
        })
      } else {
        let data = {
          "pid": subPid ? subPid : pid, //父级评论id
          "fid": this.data.detail.id, //帖子id
          "type": this.data.type, //评论类型
          "content": this.data.value, //评论内容
          replyId: this.data.level3CommentsId, //三级评论id
        }
        //文字
        api.textReview(data).then(res => {
          wx.showToast({
            title: res.data.message,
          })
          this.setData({
            isShowInput: false,
          })
          // if (this.data.currentComments.id) {
          //   //二级评论
          //   let arr = this.data.list;
          //   let index = this.data.currentIndex;
          //   console.log(arr, index)
          //   arr[index].remnant = ++arr[index].remnant;
          //   let currentComments = this.data.currentComments; //当前评论
          //   console.log('回复一级评论')
          //   // 是否展开二级评论弹层
          //   if (this.data.isShowMore) {
          //     console.log('在二级评论直接回复一级评论')
          //     if (this.data.subIndex >= 0) {
          //       console.log('回复二级评论')
          //       arr[index].listForumCommentPageVo[this.data.subIndex].listForumCommentPageVo.unshift(res.data.data);
          //       currentComments.listForumCommentPageVo[this.data.subIndex].listForumCommentPageVo.unshift(res.data.data);
          //       //三级评论
          //       this.setData({
          //         list: arr,
          //         isShowInput: false,
          //         value: '',
          //         currentComments,
          //         subComments: {},
          //         subIndex: -1,
          //       })
          //     } else {
          //       arr[index].listForumCommentPageVo.unshift(res.data.data);
          //       currentComments.listForumCommentPageVo.unshift(res.data.data);
          //       console.log('在二级评论回复一级品论')
          //       //二级评论
          //       this.setData({
          //         list: arr,
          //         isShowInput: false,
          //         value: '',
          //         currentComments,
          //         subComments: {},
          //       })
          //     }
          //   } else {
          //     console.log('直接回复一级评论')
          //     console.log(arr[index])
          //     //直接在一级评论回复
          //     arr[index].listForumCommentPageVo.unshift(res.data.data);
          //     this.setData({
          //       list: arr,
          //       isShowInput: false,
          //       value: '',
          //     })
          //   }

          // } else {
          //   //一级评论
          //   let arr = this.data.list;
          //   arr.unshift(res.data.data)
          //   this.setData({
          //     list: arr,
          //     isShowInput: false,
          //     value: '',
          //     ['detail.replyNum']: ++this.data.detail.replyNum
          //   })

          // }
        })
      }
    },
    //输入框输入的内容
    bindinput(e) {
      this.setData({
        value: e.detail.value
      })
    },
    //移除图片
    delImg(e) {
      let index = e.currentTarget.dataset.index;
      let list = this.data.tempFilePaths;
      list.splice(index, 1)
      this.setData({
        tempFilePaths: list
      })
      console.log(list);
    },
    //选择图片
    chooseImage() {
      wx.chooseImage({
        count: 3,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths
          console.log(res)
          let num = this.data.tempFilePaths.length + tempFilePaths.length;
          if (num > 3) {
            wx.showToast({
              icon: 'none',
              title: '最多可以上传3张',
            })
          } else {
            this.setData({
              tempFilePaths: this.data.tempFilePaths.concat(tempFilePaths)
            })
          }

        }
      })
    },
    //关闭输入框
    onHiddeInput() {
      this.setData({
        isShowInput: false
      })
    },
    //显示输入框
    onShowInput() {
      this.setData({
        isShowInput: true
      })
    },
    //触底时
    bindscrolltolower() {
      !this.data.isLoading && !this.data.isComplete && this.getList();
    },

    //关闭弹层
    close() {
      this.setData({
        isShowMore: false,
        currentComments: {},
        currentIndex: '',
      })
    },
    //看楼主获取全部
    ownerAll(e) {
      let type = e.currentTarget.dataset.type;
      this.setData({
        [`formData.eid`]: type,
        [`formData.pageNo`]: 1,
        list: [],
        isLoading: false,
        isComplete: false,

      })
      this.getList();

    },
    //查看更多评论
    viewMore(e) {
      let item = e.currentTarget.dataset;

      this.setData({
        isShowMore: true,
        currentComments: item.item,
        currentIndex: item.index
      })
    },
    //二级评论点赞
    layerLikeOrCancel(e) {
      let data = e.currentTarget.dataset;
      let index = this.data.currentIndex; //一级评论下标
      api.level2LikeOrCancel(data.id).then(res => {
        let list = this.data.list;
        let item = [];
        //判断是否点赞弹层的二级评论  有index是  
        if (data.index >= 0) {
          item = list[index].listForumCommentPageVo[data.index];
          if (item.isPraise) {
            --item.praiseNum;
          } else {
            ++item.praiseNum;
          }
          item.isPraise = !item.isPraise;
          list[index].listForumCommentPageVo[data.index] = item;
          let content = this.data.currentComments.listForumCommentPageVo;
          content[data.index].isPraise = item.isPraise;
          content[data.index].praiseNum = item.praiseNum;
          this.setData({
            list,
            [`currentComments.listForumCommentPageVo`]: content,
          })

        } else {
          item = list[index];
          if (item.isPraise) {
            --item.praiseNum;
          } else {
            ++item.praiseNum;
          }
          item.isPraise = !item.isPraise;
          list[index] = item;
          //弹层的一级评论
          this.setData({
            list,
            [`currentComments.isPraise`]: item.isPraise,
            [`currentComments.praiseNum`]: item.praiseNum,
          })
        }
      })
    },

    //评论点赞、取消
    level2LikeOrCancel(e) {
      let data = e.currentTarget.dataset;
      api.level2LikeOrCancel(data.id).then(res => {
        let list = this.data.list;
        let item = list[data.index];
        if (item.isPraise) {
          --item.praiseNum;
        } else {
          ++item.praiseNum;
        }
        item.isPraise = !item.isPraise;
        list[data.index] = item;
        this.setData({
          list
        })
      })
    },

    //点赞/取消帖子点赞
    giveThumbsUp() {
      api.likeOrCancel({
        id: this.data.detail.id,
        type: this.data.type
      }).then(() => {
        let data = this.data.detail;
        if (data.isPraise) {
          --data.praiseNum;
        } else {
          ++data.praiseNum;
        }
        data.isPraise = !data.isPraise;
        this.setData({
          detail: data
        })
      })
    },
    //获取评论列表
    getList() {
      this.setData({
        isLoading: true
      })
      api.getCommentList(this.data.formData).then(res => {
        this.setData({
          isLoading: false,
          [`formData.pageNo`]: ++this.data.formData.pageNo,
          list: this.data.list.concat(res.data.data.records),
          isComplete: !res.data.data.records.length
        })
      })
    },
  }
})